
//
// 110305 - AH - (Fantoft) Created.
// 110306 - AH - (Fantoft Club) setPopulate(boolean), getPopulate() and
//               populate(Lines).
// 110310 - AH - getSpokeCount() and getSpoke(int).
// 110423 - AH - New getPit() SoekratData method, return null here.
// 110617 - AH - getSpoke(Class<?>).
// 110716 - AH - (Bien *) getMotorConfiguration() that returns null.
//

package org.soekrat.engine.sparewheel;

import java.io.CharArrayWriter;
import java.io.PrintWriter;

import org.soekrat.engine.forest.Forest;
import org.soekrat.engine.io.SoekratData;
import org.soekrat.engine.mine.Mine;
import org.soekrat.engine.motor.passingstrategies.SequenceWheelProcessing;
import org.soekrat.engine.network.Network;
import org.soekrat.engine.ontology.OntologyDatastructure;
import org.soekrat.engine.ontology.ptable.ProductTable;
import org.soekrat.engine.pit.Pit;
import org.soekrat.engine.sparewheel.ontology.ForestSpareWheelOntology;
import org.soekrat.engine.sparewheel.ontology.MineSpareWheelOntology;
import org.soekrat.engine.sparewheel.ontology.NetworkSpareWheelOntology;
import org.soekrat.engine.sparewheel.ontology.SpareWheelOntology;
import org.soekrat.engine.swnetwork.SpareWheelNetwork;
import org.soekrat.engine.visitor.Lines;

 /**
 * <p>
 *   SpareWheel.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class SpareWheel implements SoekratData 
{
  private String m_title=null;
  
  private final MineSpareWheelOntology m_mineSpoke;
  
  private final NetworkSpareWheelOntology m_networkSpoke;
  
  private final ForestSpareWheelOntology m_forestSpoke;
  
  private boolean m_populate=true;
  
  
  
  @Override
  public OntologyDatastructure getOntologyDatastructure(String name) 
  {
    // TODO
    return null;
  }

  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public SpareWheel()
  {
    m_mineSpoke=new MineSpareWheelOntology();  
    m_networkSpoke=new NetworkSpareWheelOntology();
    m_forestSpoke=new ForestSpareWheelOntology();
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param sd {@link SoekratData}.
   */
  public SpareWheel(SoekratData sd)
  {
    if (sd==null)
    {
      throw new NullPointerException("sd");
    }
    
    m_mineSpoke=new MineSpareWheelOntology(sd.getMine());  
    m_networkSpoke=new NetworkSpareWheelOntology(sd.getSpareWheelNetwork());
    m_forestSpoke=new ForestSpareWheelOntology(sd.getForest());
  }
  
  /**
   * <p>
   *   Sets if should populate ontologies.
   * </p>
   * @param v {@code true} if to (default), {@code false} if not to.
   */
  public void setPopulate(boolean v){ m_populate=v; }
  
  /**
   * <p>
   *   Gets if populates ontologies.
   * </p>
   * @return {@code true} if does (default), {@code false} if does not.
   */
  public boolean getPopulate(){ return m_populate; }
  
  /**
   * <p>
   *   Populate the ontologies {@code this} access.
   * </p>
   * <p>
   *   If 
   *   {@link #getPopulate()} is {@code false} this does nothing. Default is to
   *   populate.
   * </p>
   * @param lines {@link Lines}.
   */
  public void populate(Lines lines)
  {
    if (lines==null)
    {
      throw new NullPointerException("lines");  
    }
    
    if (m_populate)
    {
      m_mineSpoke.populate(lines);
      m_networkSpoke.populate(lines);
      m_forestSpoke.populate(lines);
    }
  }
  
  /**
   * <p>
   *   Gets the number of spokes the spare wheel has.
   * </p>
   * @return Count.
   */
  public int getSpokeCount(){ return 3; }
  
  /**
   * <p>
   *   Gets spoke.
   * </p>
   * @param idx Index of spoke to get.
   * @return Spoke.
   * @throws IndexOutOfBoundsException If {@code idx<0 : idx>=getSpokeCount()}.
   */
  public SpareWheelOntology getSpoke(int idx)
  {
    switch (idx)
    {
      case 0 : return m_mineSpoke;
      case 1 : return m_networkSpoke;
      case 2 : return m_forestSpoke;
    }
    throw new IndexOutOfBoundsException(Integer.toString(idx));
  }
  
  /**
   * <p>
   *   Gets spoke. 
   * </p>
   * @param clazz {@link Class} of spoke to get.
   * @return {@link SpareWheelOntology} or {@code null} if not found.
   */
  public SpareWheelOntology getSpoke(Class<?> clazz)
  {
    if (clazz==MineSpareWheelOntology.class)    return m_mineSpoke;
    if (clazz==NetworkSpareWheelOntology.class) return m_networkSpoke;
    if (clazz==ForestSpareWheelOntology.class)  return m_forestSpoke;
    return null;
  }
  
  @Override
  public String toString() 
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    pw.println("Mine:");
    pw.println();
    pw.println(m_mineSpoke);
    
    pw.println("Network:");
    pw.println();
    pw.println(m_networkSpoke);
    
    pw.println("Forest:");
    pw.println();
    pw.println(m_forestSpoke);
    
    pw.flush();
    return caw.toString();
  }
  
  // Specified in SoekratData.
  //public MotorConfiguration getMotorConfiguration(){ return null; }
  // Specified in SoekratData.
 public SequenceWheelProcessing getSequenceWheelProcessing(){ return null; }
  
  // Specified in SoekratData.
  public Mine getMine(){ return m_mineSpoke.getMine(); }
  
  // Specified in SoekratData.
  public Network getNetwork(){ return null; }
  
  // Specified in SoekratData.
  public SpareWheelNetwork getSpareWheelNetwork()
  { 
    return m_networkSpoke.getNetwork(); 
  }

  // Specified in SoekratData.
  public Forest getForest(){ return m_forestSpoke.getForest(); }
  
  public ProductTable getProductTable(){ return null; }
  
  // Specified in SoekratData.
  public Pit getPit(){ return null; }

  // Specified in SoekratData.
  public String getName(){ return "spare"; }
  
  /**
   * <p>
   *   Sets title that should be used for data set.
   * </p>
   * @param title Title, if {@code null} or the empty 
   *              {@code String}
   *              {@link #getTitle()} will return 
   *              {@link #getName()} else returns {@code title}.
   */
  public void setTitle(String title)
  {
    m_title=(title==null) ? "" : title.trim();
    m_title=(title.length()==0) ? null : title; 
  }
  
  // Specified in SoekratData.
  public String getTitle()
  {
    return (m_title==null) ? getName() : m_title;
  }
  
}
